package com.ruoyi.web.controller.finance;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.finance.domain.DTO.KltPurchaseBatchDTO;
import com.ruoyi.finance.domain.KltPurchaseBatch;
import com.ruoyi.finance.domain.KltSalesOrder;
import com.ruoyi.finance.service.KltSalesOrderService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import static com.ruoyi.common.utils.PageUtils.startPage;

/**
 * (KltSalesOrder)表控制层
 *
 * @author makejava
 * @since 2024-12-19 12:18:03
 */
@RestController
@RequestMapping("finance/kltSalesOrder")
public class KltSalesOrderController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private KltSalesOrderService kltSalesOrderService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param kltSalesOrder 查询实体
     * @return 所有数据
     */
    @GetMapping("list")
    public TableDataInfo selectAll(Page<KltSalesOrder> page, KltSalesOrder kltSalesOrder) {
        startPage();
        QueryWrapper<KltSalesOrder> queryWrapper = new QueryWrapper<>(kltSalesOrder);
        queryWrapper.eq("is_del","0");
        List<KltSalesOrder> page1 = kltSalesOrderService.list(queryWrapper);
        return getDataTable(page1);
    }
    @GetMapping("/selectAll")
    public AjaxResult selectAll( KltSalesOrder KltSalesOrder)
    {
        QueryWrapper<com.ruoyi.finance.domain.KltSalesOrder> kltSalesOrderQueryWrapper = new QueryWrapper<>(KltSalesOrder);
        kltSalesOrderQueryWrapper.eq("is_del","0");
        return success(kltSalesOrderService.getAllKltSalesOrder(kltSalesOrderQueryWrapper));
    }
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public AjaxResult selectOne(@PathVariable Serializable id) {
        return success(this.kltSalesOrderService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param kltSalesOrder 实体对象
     * @return 新增结果
     */
    @PostMapping
    public AjaxResult insert(@RequestBody KltSalesOrder kltSalesOrder) {
        return success(this.kltSalesOrderService.save(kltSalesOrder));
    }

    /**
     * 修改数据
     *
     * @param kltSalesOrder 实体对象
     * @return 修改结果
     */
    @PutMapping
    public AjaxResult update(@RequestBody KltSalesOrder kltSalesOrder) {
        return success(this.kltSalesOrderService.updateById(kltSalesOrder));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public AjaxResult delete(@RequestParam("idList") List<Long> idList) {
        return success(this.kltSalesOrderService.updateSalesOrderIsDel(idList));
    }

    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<KltSalesOrder> util = new ExcelUtil<KltSalesOrder>(KltSalesOrder.class);
        List<KltSalesOrder> KltSalesOrder1 = util.importExcel(file.getInputStream());
        String operName = getUsername();
        for (KltSalesOrder kltSalesOrder : KltSalesOrder1) {
            kltSalesOrder.setIsDel("0");
            kltSalesOrder.setCreateSource(operName);
            kltSalesOrder.setUpdateUser(operName);
        }
        boolean b = kltSalesOrderService.saveBatch(KltSalesOrder1);
        String message="";
        if(b){
            message = "导入成功";
        }
        return success(message);
    }
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<KltSalesOrder> util = new ExcelUtil<KltSalesOrder>(KltSalesOrder.class);
        util.importTemplateExcel(response, "销售批次");
    }
    @PostMapping("/export")
    public void export(HttpServletResponse response,  KltSalesOrder kltSalesOrder)
    {
        QueryWrapper<KltSalesOrder> queryWrapper = new QueryWrapper<>(kltSalesOrder);
        queryWrapper.eq("is_del","0");
        List<KltSalesOrder> page1 = kltSalesOrderService.list(queryWrapper);
        List<KltPurchaseBatchDTO> list1 = new ArrayList<>();
        ExcelUtil<KltSalesOrder> util = new ExcelUtil<KltSalesOrder>(KltSalesOrder.class);
        util.exportExcel(response,page1,"销售批次");
    }

    @GetMapping("/selectAllCustomerName")
    public AjaxResult selectAllCustomerName(){
        List<String> list = kltSalesOrderService.selectAllCustomerName();
        return success(list);
    }
}

